package com.company.cloud.user.client;

import cn.hutool.core.bean.BeanUtil;
import com.company.cloud.captcha.modules.util.StringUtils;
import com.company.cloud.common.asserts.ValidatorUtils;
import com.company.cloud.common.bean.BaseClientResult;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.user.constant.UserConstant;
import com.company.cloud.user.modules.api.request.PermissionAddRequest;
import com.company.cloud.user.modules.api.request.PermissionGet2DelRequest;
import com.company.cloud.user.modules.api.request.PermissionQueryRequest;
import com.company.cloud.user.modules.api.request.PermissionUpdateRequest;
import com.company.cloud.user.modules.api.response.PermissionQueryResp;
import com.company.cloud.user.modules.api.response.PermissionModel;
import com.company.cloud.user.modules.api.response.UserBaseResp;
import com.company.cloud.user.modules.api.service.PermissionPlatformRemoteService;
import com.company.cloud.user.modules.mgr.controller.req.PermissionSaveParam;
import com.company.cloud.user.modules.mgr.controller.req.PermissionUpdateParam;
import com.company.cloud.user.modules.mgr.service.PermissionMgrService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Service
public class PermissionPlatformRemoteServiceImpl implements PermissionPlatformRemoteService {


    @Autowired
    private PermissionMgrService permissionMgrService;

    @Override
    public BaseClientResult<PermissionQueryResp> queryPermission(PermissionQueryRequest request) {
        try {
            ValidatorUtils.validate(request);
            List<PermissionModel> items= permissionMgrService.tree();
            PermissionQueryResp data=new PermissionQueryResp();
            data.setItems(items);
            return BaseClientResult.success(data);
        } catch (Exception e) {
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<PermissionModel> addPermission(PermissionAddRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());
            userInfo.setIp(request.getOpIp());

            PermissionSaveParam params=new PermissionSaveParam();
            BeanUtil.copyProperties(request,params);
            if(StringUtils.isEmpty(request.getParentCode())){
                params.setParentCode(UserConstant.PERMISSION_TOP_CODE);
            }

            PermissionModel data= permissionMgrService.save(params,userInfo);
            return BaseClientResult.success(data);
        } catch (Exception e) {
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<PermissionModel> updatePermission(PermissionUpdateRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());
            userInfo.setIp(request.getOpIp());

            PermissionUpdateParam params=new PermissionUpdateParam();
            BeanUtil.copyProperties(request,params);
            PermissionModel data= permissionMgrService.update(params,userInfo);
            return BaseClientResult.success(data);
        } catch (Exception e) {
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<PermissionModel> infoPermission(PermissionGet2DelRequest request) {
        try {
            ValidatorUtils.validate(request);
            PermissionModel data= permissionMgrService.get(request.getCode());
            return BaseClientResult.success(data);
        } catch (Exception e) {
            return BaseClientResult.error(e);
        }
    }

    @Override
    public BaseClientResult<String> delPermission(PermissionGet2DelRequest request) {
        try {
            ValidatorUtils.validate(request);
            LoginSimpleUserInfo userInfo=new LoginSimpleUserInfo();
            userInfo.setId(request.getOpUserId());
            userInfo.setIp(request.getOpIp());
            permissionMgrService.delete(request.getCode(),userInfo);
            return BaseClientResult.success();

        } catch (Exception e) {
            return BaseClientResult.error(e);
        }
    }
}
